#include "CheckingAccount.hh"
#include <fstream>
#include <iomanip>
#include <iostream>

CheckingAccount::CheckingAccount(const std::string& id, const std::string& owner, double initialBalance)
    : BankAccount(id, owner, initialBalance) {}

bool CheckingAccount::deposit(double amount, const std::string& note) {
    if (amount <= 0 || amount > 1000000.0) {
        std::cout << "Error: Invalid deposit amount (must be positive and less than 1,000,000).\n";
        return false;
    }

    balance += amount;
    addTransaction(Transaction("T" + std::to_string(Transaction::getNextId()), amount, "Deposit", note));
    std::cout << "Deposit successful! New balance: " << std::fixed << std::setprecision(2) << balance << "\n";
    return true;
}

bool CheckingAccount::withdraw(double amount, const std::string& note) {
    if (amount <= 0 || amount > balance || amount > 1000000.0) {
        std::cout << "Error: Invalid withdrawal amount (must be positive, less than balance, and less than 1,000,000).\n";
        return false;
    }

    balance -= amount;
    addTransaction(Transaction("T" + std::to_string(Transaction::getNextId()), amount, "Withdrawal", note));
    std::cout << "Withdrawal successful! New balance: " << std::fixed << std::setprecision(2) << balance << "\n";
    return true;
}

bool CheckingAccount::transferTo(BankAccount* destination, double amount, const std::string& note) {
    if (!destination) {
        std::cout << "Error: Invalid destination account.\n";
        return false;
    }
    if (amount <= 0 || amount > balance || amount > 1000000.0) {
        std::cout << "Error: Invalid transfer amount (must be positive, less than balance, and less than 1,000,000).\n";
        return false;
    }

    balance -= amount;
    destination->balance += amount;

    addTransaction(Transaction("T" + std::to_string(Transaction::getNextId()), amount, "Transfer Out", note));
    destination->addTransaction(Transaction("T" + std::to_string(Transaction::getNextId()), amount, "Transfer In", note));

    std::cout << "Transfer successful! New balance: " << std::fixed << std::setprecision(2) << balance << "\n";
    return true;
}
